from selenium.webdriver.support.ui import WebDriverWait  # 显示等待的包
from selenium import webdriver
from selenium.webdriver.support.select import Select  # 下拉列表的包
from selenium.webdriver.common.action_chains import ActionChains  # 鼠标的包
#页面基层类
class Base():
    # 初始化驱动
    def __init__(self, driver):
        self.driver=driver

    # 封装 打开网址的方法
    def open_url(self, url):
        self.driver.get(url)

    # 封装 定位方法-显示等待
    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20, 1).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)

    # 封装 点击事件
    def click(self, loc):
        self.find_element(loc).click()

    # 封装 输入事件
    def send_keys(self, loc, text):
        self.find_element(loc).send_keys(text)

    # 封装 多窗口切换事件
    def window_handles(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    # 封装 滑动事件-向下滚动xx像素
    def scrollTo(self):
        self.driver.execute_script('window.scrollTo(0,500)')
    # 封装 滑动事件-滑动到顶部
    def scroll_top(self):
        self.driver.execute_script('window.scrollTo(0,0)')
    # 封装 滑动事件-滑动到底部
    def scroll_height(self):
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')

    # 封装 鼠标悬停
    def move_to(self, loc):
        ActionChains(self.driver).move_to_element(self.find_element(loc)).perform()

    # 封装 下拉列表
    def select_text(self, loc, text):
        Select(self.find_element(loc)).select_by_visible_text(text)
    # 封装 下拉列表
    def select_value(self, loc, value):
        Select(self.find_element(loc)).select_by_value(value)
    # 封装 下拉列表
    def select_index(self, loc, n):
        Select(self.find_element(loc)).select_by_index(n)
